package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.dao.NoteInfoMapper;
import mobi.hami.huapin.dao.NoteSortMapper;
import mobi.hami.huapin.dao.UserInvoiceMapper;
import mobi.hami.huapin.dao.UserReturnGoodsLogMapper;
import mobi.hami.huapin.model.NoteInfo;
import mobi.hami.huapin.model.NoteSort;
import mobi.hami.huapin.model.UserInvoice;
import mobi.hami.huapin.model.UserReturnGoodsLog;
import mobi.hami.huapin.service.ManagementService;
import mobi.hami.huapin.util.Results;
import mobi.hami.huapin.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Create by 子龙 on 2018/9/05.
 */
@Service
public class ManagementServiceImpl implements ManagementService {

    private static final Logger logger = LoggerFactory.getLogger(ManagementServiceImpl.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    UserReturnGoodsLogMapper userReturnGoodsLogMapper;

    @Autowired
    UserInvoiceMapper userInvoiceMapper;

    @Autowired
    NoteInfoMapper noteInfoMapper;

    @Autowired
    NoteSortMapper noteSortMapper;


    public List<UserInvoice> selectUserInvoiceWitchParam(Map<String,Object> parms){
        return userInvoiceMapper.selectUserInvoiceWitchParam(parms);
    }

    public List<UserReturnGoodsLog> selectWitchParam(Map<String,Object> parms){
        return  userReturnGoodsLogMapper.selectWitchParam(parms);
    }

    /**
     *同意退货操作
     * 前提status=0,status改为1
     * 退货状态0:申请中1:同意退货2:用户已提交快递信息3:拒绝退货4:退货完成
     * @param userReturnGoodsLog_id  申请退货记录主键
     */
    public Result agreedreturngoods(Integer userReturnGoodsLog_id){
        if(null == userReturnGoodsLog_id){
            return Results.failure("退货记录唯一标识为空");
        }
        UserReturnGoodsLog userReturnGoodsLog = userReturnGoodsLogMapper.selectByPrimaryKey(userReturnGoodsLog_id);
        if(null == userReturnGoodsLog){
            return Results.failure("该记录不存在");
        }
        if(userReturnGoodsLog.getStatus() == null ||userReturnGoodsLog.getStatus() != 0){
            return Results.failure("该记录不处于退货申请状态,不可进行当前操作");
        }
        userReturnGoodsLog.setStatus(1);
        userReturnGoodsLog.setAccept_time(new Date());
        int i = userReturnGoodsLogMapper.updateByPrimaryKey(userReturnGoodsLog);
        if(i > 0){
            return Results.success("操作成功");
        }
        return Results.failure("操作失败");
    }


    /**
     *拒绝退货操作
     * 前提status=0,即处于申请状态才能拒绝退货,status改为3
     * 退货状态0:申请中1:同意退货2:用户已提交快递信息3:拒绝退货4:退货完成
     * @param userReturnGoodsLog_id  申请退货记录主键
     */
    public Result refusereturngoods(Integer userReturnGoodsLog_id){
        if(null == userReturnGoodsLog_id){
            return Results.failure("退货记录唯一标识为空");
        }
        UserReturnGoodsLog userReturnGoodsLog = userReturnGoodsLogMapper.selectByPrimaryKey(userReturnGoodsLog_id);
        if(null == userReturnGoodsLog){
            return Results.failure("该记录不存在");
        }
        if(userReturnGoodsLog.getStatus() == null ||userReturnGoodsLog.getStatus() != 0){
            return Results.failure("该记录不处于退货申请状态,不可进行当前操作");
        }
        userReturnGoodsLog.setStatus(3);
        userReturnGoodsLog.setRefuse_time(new Date());
        int i = userReturnGoodsLogMapper.updateByPrimaryKey(userReturnGoodsLog);
        if(i > 0){
            return Results.success("操作成功");
        }
        return Results.failure("操作失败");
    }

    /**
     * 退货完成操作
     * 退货状态0:申请中1:同意退货2:用户已提交快递信息3:拒绝退货4:退货完成
     * @param userReturnGoodsLog_id  申请退货记录主键
     */
    public Result completereturngoods(Integer userReturnGoodsLog_id){
        if(null == userReturnGoodsLog_id){
            return Results.failure("退货记录唯一标识为空");
        }
        UserReturnGoodsLog userReturnGoodsLog = userReturnGoodsLogMapper.selectByPrimaryKey(userReturnGoodsLog_id);
        if(null == userReturnGoodsLog){
            return Results.failure("该记录不存在");
        }
        if(userReturnGoodsLog.getStatus() == null ||userReturnGoodsLog.getStatus() != 2){
            return Results.failure("该记录不处于用户已提交快递信息状态,不可进行当前操作");
        }
        userReturnGoodsLog.setStatus(4);
        userReturnGoodsLog.setComplete_time(new Date());
        int i = userReturnGoodsLogMapper.updateByPrimaryKey(userReturnGoodsLog);
        if(i > 0){
            return Results.success("操作成功");
        }
        return Results.failure("操作失败");
    }

    /**
     * 同意开发票
     * 前提条件该发票申请处于申请状态,即status=0,改为status=1
     * 发票状态0:申请中1:已同意开发票2:发票完成(发票已经线下邮递,且用户收到)
     * @param userinvoice_id
     * @return
     */
    public  Result agreeduserinvoice(Integer userinvoice_id){
        if(null == userinvoice_id){
            return Results.failure("发票申请记录唯一标识为空");
        }
        UserInvoice userInvoice = userInvoiceMapper.selectByPrimaryKey(userinvoice_id);
        if(null == userInvoice){
            return Results.failure("该发票申请记录不存在");
        }
        if(null == userInvoice || userInvoice.getStatus() != 0){
            return Results.failure("该发票申请记录不处于申请状态,不可进行此操作");
        }
        userInvoice.setStatus(1);
        userInvoice.setAccept_time(new Date());
        int i = userInvoiceMapper.updateByPrimaryKey(userInvoice);
        if(i > 0){
            return Results.success("操作成功");
        }
        return Results.failure("操作失败");
    }


    /**
     * 发票完成操作
     * 前提条件该发票申请处于申请状态,即status=1,改为status=2
     * 发票状态0:申请中1:已同意开发票2:发票完成(发票已经线下邮递,且用户收到)
     * @param userinvoice_id
     * @return
     */
    public  Result completeuserinvoice(Integer userinvoice_id){
        if(null == userinvoice_id){
            return Results.failure("发票申请记录唯一标识为空");
        }
        UserInvoice userInvoice = userInvoiceMapper.selectByPrimaryKey(userinvoice_id);
        if(null == userInvoice){
            return Results.failure("该发票申请记录不存在");
        }
        if(null == userInvoice || userInvoice.getStatus() != 0){
            return Results.failure("该发票申请记录不处于已同意状态,不可进行此操作");
        }
        userInvoice.setStatus(2);
        userInvoice.setComplete_time(new Date());
        int i = userInvoiceMapper.updateByPrimaryKey(userInvoice);
        if(i > 0){
            return Results.success("操作成功");
        }
        return Results.failure("操作失败");
    }



    /**
     * 纸条库管理-添加纸条
     * @param  content 纸条内容
     */
    public Result addNote(String content){
        if(StringUtil.isEmpty(content)){
            return Results.failureWithStatus(2,"纸条内容不能为空");
        }
        NoteInfo sel = new NoteInfo();
        sel.setComtent(content);
        if(null != noteInfoMapper.selectOne(sel)){
            return Results.failureWithStatus(3,"该纸条内容已经存在");
        }
        Integer note_class_id = 0;
        NoteInfo nifo = new NoteInfo(content,new Date(),0,0,0L,0L,note_class_id,  0);
        int i = noteInfoMapper.insert(nifo);
        if(i > 0){
            return Results.successWithStatus(0,"添加成功!");
        }
        return Results.failureWithStatus(1,"添加失败");
    }

    /**
     * 纸条库管理-纸条列表
     */
    public List<NoteInfo> selectNoteinfosWitchParam(Map<String,Object> parms){
        return noteInfoMapper.selectNoteinfosWitchParam(parms);
    }


    /**
     *纸条库管理-添加纸条分类
     */
    public Result addNoteSort(String name){
        if(StringUtil.isEmpty(name)){
            return Results.failureWithStatus(2,"纸条分类名称不能为空");
        }
        NoteSort ns = new NoteSort();
        ns.setName(name);
        if(null != noteSortMapper.selectOne(ns)){
            return Results.failureWithStatus(3,"该纸条分类名称已经存在");
        }
        int maxNum = noteSortMapper.selectMaxOrderNum();
        NoteSort noteSort = new NoteSort(name,new Date(),0,maxNum+1,new Date(),0,System.currentTimeMillis());
        if(noteSortMapper.insert(noteSort) > 0){
            return Results.successWithStatus(0,"添加成功!");
        }
        return Results.failureWithStatus(1,"添加失败");
    }

    /**
     * 纸条管理库-纸条分类列表
     */
    public List<NoteSort> selectNoteSortsWitchParam(Map<String,Object> parms){
        return noteSortMapper.selectNoteSortsWitchParam(parms);
    }

    /**
     * 纸条库管理-纸条分类上下线
     */
    public Result updataNoteSortStatus(Integer id,Integer status){
        if( noteSortMapper.updataNoteSortStatus(id,status) > 0){
            return Results.successWithStatus(0,"操作成功!");
        }
        return  Results.failureWithStatus(1,"操作失败");
    }

    /**
     * 纸条库管理-纸条分类上下移动
     * @param  id 主键
     * @param  order_num 排序号
     * @param  type 0上移动 1 下移动
     */
    public Result updataNoteSortOrderNum(Integer id,Integer order_num,Integer type){
        if(null != type && type == 0){
            NoteSort ns = noteSortMapper.getOrderNumASC(order_num);
            if(null != ns){
                int i = noteSortMapper.updataOrderNum(id,ns.getOrder_num());
                int k = noteSortMapper.updataOrderNum(ns.getId(),order_num);
                if(i > 0 & k > 0){
                    return Results.successWithStatus(0,"上移操作成功");
                }
            }
        }
        if(null != type && type == 1){
            NoteSort nt = noteSortMapper.getOrderNumDESC(order_num);
            if(null != nt){
                int m = noteSortMapper.updataOrderNum(id,nt.getOrder_num());
                int n = noteSortMapper.updataOrderNum(nt.getId(),order_num);
                if(m>0 && n > 0){
                    return Results.successWithStatus(0,"下移操作成功");
                }
            }

        }
        return Results.failureWithStatus(1,"操作失败");
    }

}
